/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

package com.facebook.common.memory;

import com.facebook.infer.annotation.Nullsafe;

/**
 * Types of memory trim.
 *
 * <p>Each type of trim will provide a suggested trim ratio.
 *
 * <p>A {@link MemoryTrimmableRegistry} implementation sends out memory trim events with this type.
 */
@Nullsafe(Nullsafe.Mode.LOCAL)
public enum MemoryTrimType {

  /** The application is approaching the device-specific Java heap limit. */
  OnCloseToDalvikHeapLimit(0.5),

  /** The system as a whole is running critically low on memory, and app is in the foreground. */
  OnSystemMemoryCriticallyLowWhileAppInForeground(1),

  /** The system as a whole is running low of memory, and this application is in the foreground. */
  OnSystemLowMemoryWhileAppInForeground(0.5),

  /** The system as a whole is running out of memory, and this application is in the background. */
  OnSystemLowMemoryWhileAppInBackgroundLowSeverity(1),

  /** The system as a whole is running out of memory, and this application is in the background. */
  OnSystemModerateMemory(0.5),

  /** This app is moving into the background, usually because the user navigated to another app. */
  OnAppBackgrounded(1),

  /** Generated by MemoryRedManager. */
  OnJavaMemoryRed(1),

  /** Generated by MemoryRedManager. */
  OnJavaMemoryYellow(0.5),

  /** Generated by MemoryRedManager. */
  OnSystemMemoryRed(1),

  /** Generated by MemoryRedManager. */
  OnSystemMemoryYellow(0.5),

  /** Generated by MemoryRedManager for address space memory red state. */
  OnAddressSpaceMemoryRed(1),

  /** Generated by system when UI becomes hidden. */
  OnSystemUiHidden(1);

  private double mSuggestedTrimRatio;

  private MemoryTrimType(double suggestedTrimRatio) {
    mSuggestedTrimRatio = suggestedTrimRatio;
  }

  /** Get the recommended percentage by which to trim the cache on receiving this event. */
  public double getSuggestedTrimRatio() {
    return mSuggestedTrimRatio;
  }

  /** Map an integer to a MemoryTrimType enum. */
  public static MemoryTrimType fromInt(int type) {
    for (MemoryTrimType trimType : MemoryTrimType.values()) {
      if (trimType.ordinal() == type) {
        return trimType;
      }
    }
    throw new IllegalArgumentException("Unknown type: " + type);
  }
}
